home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / shareware / intuition / yak_1.57 / source / hotkey_types.c < prev    next >
C/C++ Source or Header  |  1994-11-17  |  15KB  |  717 lines

  1. /*
  2.  * Commands relating to Yak hotkey types
  3.  * mws, 27 April 1993
  4.  */
  5. #include <exec/types.h>
  6. #include <exec/libraries.h>
  7. #include <exec/memory.h>
  8. #include <devices/inputevent.h>
  9. #include <dos/dos.h>
  10. #include <dos/datetime.h>
  11. #include <dos/dostags.h>
  12. #include <graphics/displayinfo.h>
  13. #include <libraries/commodities.h>
  14. #include <libraries/reqtools.h>
  15. #include <libraries/locale.h>
  16. #include <intuition/intuitionbase.h>
  17. #include <stddef.h>
  18.  
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21. #include <proto/graphics.h>
  22. #include <proto/commodities.h>
  23. #include <proto/intuition.h>
  24. #include <proto/reqtools.h>
  25. #include <proto/locale.h>
  26.  
  27. #include <string.h>
  28.  
  29. #include "hotkey_types.h"
  30. #include "yak.h"
  31. #include "localprotos.h"
  32.  
  33. #define CATCOMP_NUMBERS
  34. #include "yak_locale_strings.h"
  35.  
  36. extern struct Library *LocaleBase;
  37.  
  38. UWORD num_hkeys;
  39.  
  40. /*
  41.  * Utility functions
  42.  */
  43. #define HOW_ZIP        0
  44. #define HOW_SHRINK     1
  45. #define HOW_ENLARGE    2
  46.  
  47. static void HandleWindowKey(int how);
  48. static void __saveds __asm putcharfunc( register __a0 struct Hook *h,
  49.                     register __a2 void *object,
  50.                     register __a1 char c);
  51. static struct InputEvent *FlipIEvents(struct InputEvent *);
  52. static int AddToStream(char *str); 
  53. static struct Window * FindWBWindow(void);
  54. static void __saveds DoPalette(void);
  55. static int palette_count;
  56. static LONG beginCommand(char *command);
  57.  
  58.  
  59. /*
  60.  * HotKey Action function prototypes
  61.  */
  62. static void     act_DosCommand(YakHotKey *yhk), act_CloseWindow(YakHotKey *yhk),
  63.         act_ZipWindow(YakHotKey *yhk), act_ShrinkWindow(YakHotKey *yhk),
  64.         act_ExpandWindow(YakHotKey *yhk), act_CycleWindows(YakHotKey *yhk),
  65.         act_OpenPalette(YakHotKey *yhk), act_ScreenToFront(YakHotKey *yhk),
  66.         act_ScreenToBack(YakHotKey *yhk), act_ActivateWorkbench(YakHotKey *yhk),
  67.         act_CentreScreen(YakHotKey *yhk), act_BlankDisplay(YakHotKey *yhk),
  68.         act_InsertText(YakHotKey *yhk), act_InsertDate(YakHotKey *yhk),
  69.         act_ShowInterface(YakHotKey *yhk), act_BackCycleWindows(YakHotKey *yhk) ;
  70.  
  71. #define EMPTY_MINLIST {NULL,NULL,NULL}
  72. YakHotKeyType yhktypes[NUM_HOTKEY_TYPES] = {
  73.  
  74.     { EMPTY_MINLIST, act_DosCommand, 0, TAKES_ARGUMENT|HAS_OPTIONS },
  75.     { EMPTY_MINLIST, act_CloseWindow, 0, 0L },
  76.     { EMPTY_MINLIST, act_ZipWindow, 0, 0L },
  77.     { EMPTY_MINLIST, act_ShrinkWindow, 0, 0L },
  78.     { EMPTY_MINLIST, act_ExpandWindow, 0, 0L },
  79.     { EMPTY_MINLIST, act_CycleWindows, 0, 0L },
  80.     { EMPTY_MINLIST, act_OpenPalette, 0, HAS_OPTIONS },
  81.     { EMPTY_MINLIST, act_ScreenToFront, 0, 0L },
  82.     { EMPTY_MINLIST, act_ScreenToBack, 0, 0L },
  83.     { EMPTY_MINLIST, act_ActivateWorkbench, 0, 0L },
  84.     { EMPTY_MINLIST, act_CentreScreen, 0, 0L },
  85.     { EMPTY_MINLIST, act_BlankDisplay, 0, 0L },
  86.     { EMPTY_MINLIST, act_InsertText, 0, TAKES_ARGUMENT },
  87.     { EMPTY_MINLIST, act_InsertDate, 0, TAKES_ARGUMENT },
  88.     { EMPTY_MINLIST, act_ShowInterface, 0, HAS_OPTIONS },
  89.     { EMPTY_MINLIST, act_BackCycleWindows, 0, 0L }
  90. };
  91.  
  92.  
  93. /*
  94.  * Utility functions
  95.  */
  96. #define AdjustedLeftEdge(w, width) \
  97.     (w->LeftEdge + width > w->WScreen->Width ? \
  98.     w->WScreen->Width - width : w->LeftEdge)
  99. #define AdjustedTopEdge(w, height) \
  100.     (w->TopEdge + height > w->WScreen->Height ? \
  101.     w->WScreen->Height - height : w->TopEdge)
  102.  
  103. /* zip/enlarge or whatever the currently active window */
  104. static void
  105. HandleWindowKey(int how)
  106. {
  107.     struct Window *window;
  108.     ULONG lock;
  109.  
  110.     lock = LockIBase(0);
  111.     if (window = IntuitionBase->ActiveWindow)
  112.     {
  113.         switch (how)
  114.         {
  115.         case HOW_ZIP:
  116.             if (window->Flags & WFLG_HASZOOM)
  117.                 ZipWindow(window);
  118.             break;
  119.  
  120.         case HOW_SHRINK:
  121.             if (window->Flags & WFLG_SIZEGADGET)
  122.                 ChangeWindowBox(window,
  123.                         window->LeftEdge,
  124.                         window->TopEdge,
  125.                         window->MinWidth,
  126.                         window->MinHeight);
  127.             break;
  128.  
  129.         case HOW_ENLARGE:
  130.             {    /* sometimes Max vars are -1 == NO LIMIT */
  131.             USHORT width, height;
  132.  
  133.             width = window->MaxWidth;
  134.             if (width == -1) width = window->WScreen->Width-width;
  135.             height = window->MaxHeight;
  136.             if (height == -1) height = window->WScreen->Height-height;
  137.             
  138.             if (window->Flags & WFLG_SIZEGADGET)
  139.                 ChangeWindowBox(window,
  140.                         AdjustedLeftEdge(window, width),
  141.                         AdjustedTopEdge(window, height),
  142.                         width,
  143.                         height);
  144.             }
  145.             break;
  146.  
  147.         } /* switch */
  148.     }
  149.     UnlockIBase(lock);
  150. }
  151.  
  152. /* hook-function for inserting chars into input-stream */
  153. static void __saveds __asm
  154. putcharfunc(    register __a0 struct Hook *h,
  155.         register __a2 void *object,
  156.         register __a1 char c)
  157. {
  158.     struct InputEvent ev;
  159.  
  160.     if (c)    /* stop at null-terminator */
  161.     {
  162.         ev.ie_NextEvent = NULL;
  163.         InvertKeyMap(c, &ev, NULL);
  164.         AddIEvents(&ev);
  165.     }
  166. }
  167. /* and its associated structure */
  168. static struct Hook putcharhook = {
  169.     {NULL, NULL},
  170.     (APTR)putcharfunc,
  171.     NULL,
  172.     NULL
  173. };
  174.  
  175. /* given a list of input events, reverse their order */
  176. /* NB: the list BETTER terminate... */
  177. static struct InputEvent *
  178. FlipIEvents(struct InputEvent *ie)
  179. {
  180.     struct InputEvent *oldtail, *newtail = NULL;
  181.  
  182.     while (ie)
  183.     {
  184.         /* remove head, set next event */
  185.         oldtail = ie->ie_NextEvent;    /* this will be prev event */
  186.         ie->ie_NextEvent = newtail;    /* add to head */
  187.         newtail = ie;
  188.         ie = oldtail;            /* what's left */
  189.     }
  190.     return newtail;
  191. }
  192.  
  193. /* adds a string to the input-stream */
  194. static int
  195. AddToStream(char *str)
  196. {
  197.     struct InputEvent *ie;
  198.  
  199.     if (ie = InvertString(str, NULL))
  200.     {
  201.         ie = FlipIEvents(ie);
  202.         AddIEvents(ie);
  203.         FreeIEvents(ie);
  204.         return TRUE;
  205.     }
  206.     return FALSE;
  207. }
  208.  
  209. /* find (a) Workbench window */
  210. static struct Window *
  211. FindWBWindow()
  212. {
  213.     struct Screen *s = LockPubScreen("Workbench");
  214.     if (s)
  215.     {
  216.     struct Window *w = s->FirstWindow;
  217.     WBenchToFront();
  218.     Forbid();
  219.     for (; w; w = w->NextWindow)
  220.         if (w->Flags & WFLG_WBENCHWINDOW)
  221.             break;
  222.     Permit();
  223.     UnlockPubScreen(NULL, s);
  224.     return w;
  225.     }
  226.     return NULL;
  227. }
  228.  
  229. /* bring a palette up on frontmost screen */
  230. /* CreateNewProc this function... */
  231. static void __saveds
  232. DoPalette()
  233. {
  234.     struct ReqToolsBase *ReqToolsBase;
  235.     LONG tags[3];
  236.  
  237.     palette_count++;
  238.     if (ReqToolsBase = (void *)OpenLibrary("reqtools.library", 0L))
  239.     {
  240.         tags[0] = RT_Screen;
  241.         tags[1] = (LONG)ReqToolsBase->IntuitionBase->FirstScreen;
  242.         tags[2] = TAG_DONE;
  243.         (void) rtPaletteRequestA("Palette", NULL, (struct TagItem *)tags);
  244.         CloseLibrary(ReqToolsBase);
  245.     }
  246.     palette_count--;
  247. }
  248.  
  249. /*
  250.  * Asynchronous external command started with its own autocon Input/Output
  251.  * Result will only reflect whether System() call itself succeeded.
  252.  * If System() call fails, result will be -1L; -2L for window failure.
  253.  * We are using -2L as result if our Open of CON: fails
  254.  */
  255. #define AUTOCON "CON:0/25/640/150/Yak Command/AUTO/CLOSE/WAIT/ALT0/25/80/50"
  256. static LONG
  257. beginCommand(char *command)
  258. {
  259.     static ULONG stags[] = {
  260.         SYS_Input, NULL,
  261.         SYS_Output, NULL,
  262.         SYS_Asynch, TRUE,
  263.         SYS_UserShell, TRUE,
  264.         NP_Priority, 0L,
  265.         TAG_DONE
  266.     };
  267.     BPTR file;
  268.     char *filename;
  269.  
  270. #ifdef NOTDEF    /* will this screw up sometimes? */
  271.     char *t;
  272.     int inquote = FALSE, usecon = TRUE;
  273.  
  274.     /* determine (simple-mindedly) whether there's redirection */
  275.     for (t = command; *t; t++)
  276.     {
  277.         if (*t == '\"')
  278.             if (t <= command || t[-1] != '*')
  279.                 inquote ^= 1;
  280.         if (!inquote)
  281.             if (*t == '>')
  282.             {
  283.                 usecon = FALSE;
  284.                 break;
  285.             }
  286.     }
  287.  
  288.     filename = usecon ? AUTOCON : "NIL:";
  289. #else
  290.     filename = AUTOCON;
  291. #endif
  292.     if (file = Open(filename, MODE_OLDFILE))
  293.         {
  294.         stags[1] = (ULONG)file;
  295.         return(SystemTagList(command, (struct TagItem *)stags));
  296.         }
  297.     else return(-2);
  298. }
  299.  
  300.  
  301. /*
  302.  * the hotkey actions themselves
  303.  */
  304.  
  305. /* asynchronously execute a dos command */
  306. static void
  307. act_DosCommand(YakHotKey *yhk)
  308. {
  309.     if (yhk->yhk_ArgStr)
  310.     {
  311.         LONG rc = beginCommand(yhk->yhk_ArgStr);
  312.  
  313.         if ((rc == -1) || (rc == -2))
  314.             PostError("%s\n\"%s\"",
  315.                       getString(Couldnt_start_ps_for_cmd_ERR), 
  316.                       yhk->yhk_ArgStr);
  317.     }
  318.     else PostError("%s\"%s\".", 
  319.                    getString(No_cmd_specified_for_hotkey_ERR), yhk->yhk_KeyDef);
  320. }
  321.  
  322. static void
  323. act_CloseWindow(YakHotKey *yhk)
  324. {
  325.     struct InputEvent ev;
  326.     struct Window *window;
  327.     ULONG lock;
  328.  
  329.     lock = LockIBase(0);
  330.     window = IntuitionBase->ActiveWindow;
  331.     UnlockIBase(lock);
  332.  
  333.     if (window && (window->Flags & CLOSEGADGET))
  334.     {
  335.         ev.ie_NextEvent = NULL;
  336.         ev.ie_Class = IECLASS_CLOSEWINDOW;
  337.         ev.ie_SubClass = 0;
  338.         ev.ie_Code = 0;
  339.         ev.ie_Qualifier = 0;
  340.         ev.ie_EventAddress = 0;
  341.         AddIEvents(&ev);
  342.     }
  343. }
  344.  
  345. static void
  346. act_ZipWindow(YakHotKey *yhk)
  347. {
  348.     HandleWindowKey(HOW_ZIP);
  349. }
  350.  
  351. stati